

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;

/**
 * Servlet implementation class ConnectServlet
 */
public class ConnectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConnectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	System.out.println("4");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	System.out.println("555");
	
	  // Long group_id=Long.parseLong(request.getParameter("group"));
	String user_name=request.getParameter("username");
	Continuation cc;
	 int length;
	synchronized(ConnectionsHolder.getInstance())
	{
	  // Long group_id=ConnectionsHolder.getInstance().last_group;
	   //Continuation
	   cc = ContinuationSupport.getContinuation(request, null);
	   length= ConnectionsHolder.getInstance().connections.size();
       ConnectionsHolder.getInstance().connections.add(cc);
       ConnectionsHolder.getInstance().users.add(user_name);
       ConnectionsHolder.getInstance().booleans.add(true);
	}
       System.out.println("123");
       cc.suspend(0);
       
       
       System.out.println("ggg");
      
       if(ConnectionsHolder.getInstance().booleans.get(length))
       response.getWriter().print("uploaded:"+ConnectionsHolder.getInstance().last_file_id+","+ConnectionsHolder.getInstance().last_file_path+","+ConnectionsHolder.getInstance().last_group);
       else
    	response.getWriter().print("stopped");
       
       synchronized(ConnectionsHolder.getInstance())
       {
       ConnectionsHolder.getInstance().connections.remove(cc);
       ConnectionsHolder.getInstance().users.remove(length);
       ConnectionsHolder.getInstance().booleans.remove(length);
       }
       System.out.println("ovwr");
	}

}
